package com.ysqcould.Community.service.impl;


import com.ysqcould.Community.domain.DonationsActivity;
import com.ysqcould.Community.domain.SocDonationAmount;
import com.ysqcould.Community.domain.SocUsefulActivity;
import com.ysqcould.Community.domain.vo.DoActivityVo;
import com.ysqcould.Community.mapper.DonationsActivityMapper;
import com.ysqcould.Community.mapper.SocDonationAmountMapper;
import com.ysqcould.Community.mapper.SocUsefulActivityMapper;
import com.ysqcould.Community.service.IDonationsActivityService;
import com.ysqcould.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 公益捐助二维码Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-06-30
 */
@Service
public class DonationsActivityServiceImpl implements IDonationsActivityService
{
    @Autowired
    private DonationsActivityMapper donationsActivityMapper;

    @Resource
    private SocDonationAmountMapper  donationAmountMapper;


    @Resource
    private SocUsefulActivityMapper usefulActivityMapper;

    /**
     * 查询公益捐助二维码
     * 
     * @param id 公益捐助二维码主键
     * @return 公益捐助二维码
     */
    @Override
    public DonationsActivity selectDonationsActivityById(Long id)
    {
        return donationsActivityMapper.selectDonationsActivityById(id);
    }

    /**
     * 查询公益捐助二维码列表
     * 
     * @param donationsActivity 公益捐助二维码
     * @return 公益捐助二维码
     */
    @Override
    public List<DoActivityVo> selectDonationsActivityList(DonationsActivity donationsActivity)
    {
        final List<DoActivityVo> activities = donationsActivityMapper.selectDonationsActivityList(donationsActivity);
         activities.forEach(p->{

             if (donationsActivity.getType()==1){
                 SocUsefulActivity usefulActivity=new SocUsefulActivity();
                 usefulActivity.setActivityId(p.getId());
                 final long count = usefulActivityMapper.selectSocUsefulActivityList(usefulActivity).stream().count();
                 p.setNum(count);
             }else {
                 SocDonationAmount amount=new SocDonationAmount();
                 amount.setActivityId(p.getId());
                 amount.setPayStatus(1L);
                 final List<SocDonationAmount> list = donationAmountMapper.selectSocDonationAmountList(amount);
                 p.setNum(list.stream().count());
             }

         });
        return activities;
    }

    /**
     * 新增公益捐助二维码
     * 
     * @param donationsActivity 公益捐助二维码
     * @return 结果
     */
    @Override
    public int insertDonationsActivity(DonationsActivity donationsActivity)
    {
        donationsActivity.setCreateTime(DateUtils.getNowDate());
        return donationsActivityMapper.insertDonationsActivity(donationsActivity);
    }

    /**
     * 修改公益捐助二维码
     * 
     * @param donationsActivity 公益捐助二维码
     * @return 结果
     */
    @Override
    public int updateDonationsActivity(DonationsActivity donationsActivity)
    {
        donationsActivity.setUpdateTime(DateUtils.getNowDate());
        return donationsActivityMapper.updateDonationsActivity(donationsActivity);
    }

    /**
     * 批量删除公益捐助二维码
     * 
     * @param ids 需要删除的公益捐助二维码主键
     * @return 结果
     */
    @Override
    public int deleteDonationsActivityByIds(Long[] ids)
    {
        return donationsActivityMapper.deleteDonationsActivityByIds(ids);
    }

    /**
     * 删除公益捐助二维码信息
     * 
     * @param id 公益捐助二维码主键
     * @return 结果
     */
    @Override
    public int deleteDonationsActivityById(Long id)
    {
        return donationsActivityMapper.deleteDonationsActivityById(id);
    }
}
